package yf.mall.controller.api;

import com.yf.annotation.LoginUserId;
import com.yf.response.RRException;
import yf.mall.entity.UserEntity;
import com.yf.response.R;
import yf.mall.enums.UserStatusEnum;
import yf.mall.service.MallOrderService;
import yf.mall.service.MallStoreService;
import yf.mall.service.UserService;
import yf.mall.vo.MallOrderVo;
import yf.mall.vo.MallStoreVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RequestMapping("/api/store")
@RestController
public class ApiStoreController {

    @Autowired
    private MallStoreService storeService;

    @Autowired
    private MallOrderService mallOrderService;

    /**
     * 找出某人的产品账户
     *
     * @param userId
     * @return
     */
    @RequestMapping("/list")
    public R list(@LoginUserId Long uid, Long userId) {
        if (userId == null) {
            userId = uid;
        }
        List<MallStoreVo> storeVos = storeService.findByUserId(userId);
        return R.ok(storeVos);
    }


    /**
     * 转货/发货
     *
     * @param userId
     * @param mallOrderVo
     * @return
     */
    @RequestMapping("/transfer")
    public R transfer(@LoginUserId Long userId, @RequestBody MallOrderVo mallOrderVo) {
        userService.validateUser(userId);
        UserEntity userEntity = userService.get(userId);
        mallOrderVo.setFromUser(userEntity);
        mallOrderService.transfer(mallOrderVo);
        return R.ok();
    }

    @Autowired
    private UserService userService;

}
